﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyKho.Server;

namespace QuanLyKho.Server.BLL
{
    public class PhieuNhapThuocBLL
    {
        public PhieuNhapThuocBLL()
        {
        }

        public int CreatePhieuNhap(PhieuNhapThuoc phieuNhap)
        {
            int result;
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                entities.PhieuNhapThuocs.Add(phieuNhap);
                result = entities.SaveChanges();
            }
            return result;
        }

        public void UpdatePhieuNhap(PhieuNhapThuoc phieuNhap)
        {
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                PhieuNhapThuoc phieuNhapCurrent = entities.PhieuNhapThuocs.SingleOrDefault(u=>u.IdPhieuNhap == phieuNhap.IdPhieuNhap);
                phieuNhapCurrent.MaNhaPhanPhoi = phieuNhap.MaNhaPhanPhoi;
                phieuNhapCurrent.SoHoaDon = phieuNhap.SoHoaDon;
                phieuNhapCurrent.NgayNhap = phieuNhap.NgayNhap;
                phieuNhapCurrent.LaPhieuTra = phieuNhap.LaPhieuTra;
                phieuNhapCurrent.ChietKhau = phieuNhap.ChietKhau;
                entities.SaveChanges();
            }
        }

        public List<PhieuNhapThuoc> RetrieveAll()
        {
            List<PhieuNhapThuoc> list = new List<PhieuNhapThuoc>();
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.PhieuNhapThuocs.Include("Dm_NhaPhanPhoi").OrderByDescending(u=>u.IdPhieuNhap).ToList();
            }
            return list;
        }

        public List<PhieuNhapThuoc> RetrieveTop10()
        {
            List<PhieuNhapThuoc> list = new List<PhieuNhapThuoc>();
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.PhieuNhapThuocs.Include("Dm_NhaPhanPhoi").OrderByDescending(u => u.IdPhieuNhap).Take(10).ToList();
            }
            return list;
        }

        public List<PhieuNhapThuoc> RetrievePhieuNhapByDate(DateTime tuNgay, DateTime denNgay)
        {
            tuNgay = Convert.ToDateTime(tuNgay.ToShortDateString() + " 00:00:00");
            denNgay = Convert.ToDateTime(denNgay.ToShortDateString() + " 23:59:59");
            List<PhieuNhapThuoc> list = new List<PhieuNhapThuoc>();
            using (QuanLyKhoEntities entities = new QuanLyKhoEntities(QuanLyKhoServerGlobal.ConnectionString))
            {
                list = entities.PhieuNhapThuocs.Include("Dm_NhaPhanPhoi").Where(u => u.NgayNhap >= tuNgay && u.NgayNhap <= denNgay).OrderByDescending(u => u.IdPhieuNhap).ToList();
            }
            return list;
        }
    }
}
